package utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import play.Logger;
import play.cache.Cache;

/**
 * 日历工具
 * 
 * @author ultrafrog
 * @version 1.0, 2014-11-07
 * @since 1.0
 */
public abstract class Calendars {

	private static final String CACHE_PREFIX	= "play_weixin_cache_calendars_pattern_";
	
	/**
	 * @param pattern
	 * @return
	 */
	public static DateFormat getFormat(String pattern) {
		DateFormat format = Cache.get(CACHE_PREFIX + pattern, DateFormat.class);
		if (format == null) {
			format = new SimpleDateFormat(pattern);
			Cache.add(CACHE_PREFIX + pattern, format);
			Logger.info("cache calendar pattern: %s", pattern);
		}
		return format;
	}
	
	/**
	 * @param pattern
	 * @param date
	 * @return
	 */
	public static String format(String pattern, Date date) {
		return getFormat(pattern).format(date);
	}
	
	/**
	 * @param pattern
	 * @return
	 */
	public static String format(String pattern) {
		return format(pattern, new Date());
	}
}
